<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: num1 == index }"
            v-for="(item, index) in optionsList1"
            :key="index"
            @click="didFn1(item.min, item.max, index)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: num2 == index }"
            v-for="(item, index) in optionsList2"
            :key="index"
            @click="typeFn2(item.type, index)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
    </div>
    <div style="text-align:center;margin-top:40px">
      <i :class="isReachBottom?'':'iconfont icon-loading loading'"></i>
      {{ isReachBottom ? "已经到底啦~~~" : "加载中~请稍后~~" }}</div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { goodSearchAPI } from "@/request/api";
import store from "@/store";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      goodsList: [],
      optionsList1: [
        { min: 0, max: 0, txt: "全部" },
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500个" },
        { min: 500, max: 1000, txt: "500-1000个" },
        { min: 1000, max: 1500, txt: "1000-1500个" },
        { min: 1500, max: 2500, txt: "1500-2500个" },
        { min: 2500, max: 6500, txt: "2500-6500个" },
        { min: 6500, max: 10000, txt: "6500-10000个" },
      ],
      optionsList2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      num1: 0,
      num2: 0,
      goodsListShow: [],
      isLoading: false,
      isReachBottom: false,
      page: 1,
      size: 8,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    this.keyword = this.$route.query.keyword || "";

    this.getGoodSearch();
  },
  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    async getGoodSearch() {
      let res = await goodSearchAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      this.goodsList = res.data;
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);
      this.isReachBottom = false;
      if (this.goodsListShow.length == this.goodsList.length) {
        this.isReachBottom = true;
        return;
      }
      console.log(this.isReachBottom);
    },
    didFn1(min, max, idx) {
      this.min = min;
      this.max = max;
      this.num1 = idx;
      this.getGoodSearch();
      console.log(min, max);
    },
    typeFn2(type, idx) {
      this.type = type;
      this.num2 = idx;
      this.getGoodSearch();
    },
    scrollFn() {
      console.log(
        "gin!!",
        getScrollTop(),
        getClientHeight(),
        getScrollHeight()
      );
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 270) {
        console.log("可以加载了");
        if (this.goodsListShow.length == this.goodsList.length) {
          this.isReachBottom = true;
          return;
        }
        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          setTimeout(() => {
            for (
              let i = (this.page - 1) * this.size;
              i < this.page * this.size;
              i++
            ) {
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            this.isLoading = false;
          }, 1500);
        }
      }
    },
  },
  // watch:{
  //   "$route.path.keyword":{
  //     handler(newVal,oldVal){
  //       this.keyword=newVal
  //       this.getGoodSearch();
  //     }
  //   }
  // },
  beforeRouteUpdate(to, from, next) {
    this.keyword = to.query.keyword;
    this.getGoodSearch();
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
.loading{
  display: inline-block;
  animation: loading 2s linear infinite forwards;
}
@keyframes loading{
  from{
    transform: rotate(0deg);
  }  
  to{
    transform: rotate(360deg);
  }
}
</style>